package jmars.mobile.worker.gephi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import jmars.mobile.common.Investigator;
import jmars.mobile.datainvestigation.Summary;

/**
 * select [group columns], count, sum(target column1) [, sum(target column2)...] 
 * from data group by [group columns] 
 * order by count desc
 * 
 * @author Jan
 *
 */
public class GPRSServiceEdges implements Investigator {

	@Override
	public Long compute(File[] files, FileWriter writer) throws IOException {
		Long total = 0L;
		FileInputStream fis;
		BufferedReader reader;
		String row;
		String[] cols;
		Map<String,Integer> distinct=new HashMap<String,Integer>();
		Map<String,Map<Integer,Double>> edgeMap=new HashMap<String,Map<Integer,Double>>();
		Map<Integer,Double> eachEdge;
		String eKey;
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("0");
		Double tempD;
		for(File inputFile:files){
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			while((row=reader.readLine())!=null){
				cols=row.split(Summary.columnSeperator);
				if(Summary.filterRow(cols,false,true)) continue;
				if(cols.length<20)continue;
				if(cols[18].indexOf(".")==-1)continue;
				if(cols[19].indexOf(".")==-1)continue;
				eKey=cols[18]+"#"+cols[19];
				if(!edgeMap.containsKey(eKey)){
					distinct.put(eKey,1);
					eachEdge = new HashMap<Integer,Double>();
					edgeMap.put(eKey, eachEdge);
				}else{
					eachEdge = edgeMap.get(eKey);
					distinct.put(eKey,distinct.get(eKey)+1);
				}
				for(Integer idx:Summary.targetColumnIdx){
					tempD=Double.parseDouble(cols[idx]);
					if(!eachEdge.containsKey(idx)){
						eachEdge.put(idx, tempD);
					}else{
						eachEdge.put(idx, eachEdge.get(idx)+tempD);
					}
				}
				total++;
			}
			reader.close();
			fis.close();
			System.out.println(inputFile.getName()+" finished at "+total);
		}
		System.out.print("%SGSNGGSN:"+df.format(distinct.size())+"\r\n");
		System.out.print("%total:"+df.format(total)+"\r\n");
		if(distinct.size()<=10000000){
	        System.out.print("%*****************details******************\r\n%");
			writer.write("Source,Target,Type,Id,Label,Count");
			for(Integer idx:Summary.targetColumnIdx){
				writer.write(","+Summary.columns[idx]+"");
			}
			writer.write("\r\n");
	        for(String key : edgeMap.keySet()) {
	            writer.write(key.replaceAll("#", ",")+",Directed,"+key+",,"+df.format(distinct.get(key)));
	            for(Integer idx:Summary.targetColumnIdx){
		            writer.write(","+df.format(edgeMap.get(key).get(idx)));
	            }
	            writer.write("\r\n");
	        }
		}else{
			writer.write("Too many rows, please adjust the threshold.\r\n");
		}
		return total;
	}

}
